package com.whw.fy.service.impl;


import com.whw.fy.entity.*;
import com.whw.fy.mapper.*;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

import com.whw.fy.service.PlanService;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@Service
public class PlanServiceImpl implements PlanService {

    @Resource
    private PlanMapper planMapper;
    @Resource
    private PlanSummarizeTableMapper planSummarizeTableMapper;
    @Resource
    private StudentMapper studentMapper;
    @Resource
    private StaffMapper staffMapper;
    @Resource
    private AreaMapper areaMapper;
    @Resource
    private AreaTypeMapper areaTypeMapper;

    @Override
    public Message deleteByPrimaryKey(Integer planId) {
        Plan plan = planMapper.selectByPrimaryKey(planId);
        String [] areaId = plan.getPlanAreaId().split(";");
        for (String id : areaId){
            areaMapper.deleteByPrimaryKey(Integer.parseInt(id));
        }
         String [] planSummerTableId = plan.getPlanSummarizeId().split(";");
        for (String id : planSummerTableId){
            planSummarizeTableMapper.deleteByPrimaryKey(Integer.parseInt(id));
        }
        planMapper.deleteByPrimaryKey(planId);
        return Message.success(null);
    }

    @Override
    public Message insert(Plan plan) {
        return planMapper.insert(plan) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message insertSelective(Plan plan) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        if (plan.getPlanTimeToString()!=null){
            plan.setPlanTime(simpleDateFormat.parse(plan.getPlanTimeToString()));
        }
        if(plan.getPlanExplaintimeToString() != null){
            plan.setPlanExplaintime(simpleDateFormat.parse(plan.getPlanExplaintimeToString()));
        }
        return planMapper.insertSelective(plan) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message selectByPrimaryKey(Integer planId) {
        Plan plan = planMapper.selectByPrimaryKey(planId);
        HashMap map = new HashMap();
        if (plan.getPlanStudentId() != null)
            map.put("planStudentName", studentMapper.selectByPrimaryKey(plan.getPlanStudentId()).getStudentName());
        else map.put("planStudentName", null);

        if (plan.getPlanTeacherleaderId() != null)
            map.put("planTeacherLeaderName", staffMapper.selectByPrimaryKey(plan.getPlanTeacherleaderId()).getStaffName());
        else map.put("planTeacherLeaderName", null);

        if (plan.getPlanRecoveryleaderId() != null)
            map.put("planRecoveryLeaderName", staffMapper.selectByPrimaryKey(plan.getPlanRecoveryleaderId()).getStaffName());
        else map.put("planRecoveryLeaderName", null);

        if (plan.getPlanPhysicianleaderId() != null)
            map.put("planPhysicianLeaderName", staffMapper.selectByPrimaryKey(plan.getPlanPhysicianleaderId()).getStaffName());
        else map.put("planPhysicianLeaderName", null);

        if (plan.getPlanPeopleId() != null)
            map.put("planPeopleName", staffMapper.selectByPrimaryKey(plan.getPlanPeopleId()).getStaffName());
        else map.put("planPeopleName", null);

        if (plan.getPlanExplainpeopleId() != null)
            map.put("planExplainPeopleName", staffMapper.selectByPrimaryKey(plan.getPlanExplainpeopleId()).getStaffName());
        else map.put("planExplainPeopleName", null);
            map.put("plan", plan);
            List<PlanSummarizeTable> planSummarizeTableList = new LinkedList<>();
            List<Area> areaList = new LinkedList<>();
            String[] ids = plan.getPlanSummarizeId().split(";");
            if (ids.length > 0) {
                for (String ID : ids) {
                    PlanSummarizeTable planSummarizeTable = planSummarizeTableMapper.selectByPrimaryKey(Integer.parseInt(ID));
                    planSummarizeTableList.add(planSummarizeTable);
                }
            }
            map.put("planSummarizeTableList", planSummarizeTableList);
            String[] idss = plan.getPlanAreaId().split(";");
            if (ids.length > 0) {
                for (String ID : idss) {
                    Area area = areaMapper.selectByPrimaryKey(Integer.parseInt(ID));
                    areaList.add(area);
                }

            map.put("areaList", areaList);
        }
        return Message.success(null).add(map);
    }

    @Override
    public Message selectAll() {
        List<Plan> planList = planMapper.selectAll();
        List<Map> mapList = new LinkedList<>();
        for (Plan plan : planList) {
            HashMap map = new HashMap();
            if (plan.getPlanStudentId() != null)
                map.put("planStudentName", studentMapper.selectByPrimaryKey(plan.getPlanStudentId()).getStudentName());
            else map.put("planStudentName", null);

            if (plan.getPlanTeacherleaderId() != null)
                map.put("planTeacherLeaderName", staffMapper.selectByPrimaryKey(plan.getPlanTeacherleaderId()).getStaffName());
            else map.put("planTeacherLeaderName", null);

            if (plan.getPlanRecoveryleaderId() != null)
                map.put("planRecoveryLeaderName", staffMapper.selectByPrimaryKey(plan.getPlanRecoveryleaderId()).getStaffName());
            else map.put("planRecoveryLeaderName", null);

            if (plan.getPlanPhysicianleaderId() != null)
                map.put("planPhysicianLeaderName", staffMapper.selectByPrimaryKey(plan.getPlanPhysicianleaderId()).getStaffName());
            else map.put("planPhysicianLeaderName", null);

            if (plan.getPlanPeopleId() != null)
                map.put("planPeopleName", staffMapper.selectByPrimaryKey(plan.getPlanPeopleId()).getStaffName());
            else map.put("planPeopleName", null);

            if (plan.getPlanExplainpeopleId() != null)
                map.put("planExplainPeopleName", staffMapper.selectByPrimaryKey(plan.getPlanExplainpeopleId()).getStaffName());
            else map.put("planExplainPeopleName", null);
            map.put("plan", plan);
            List<PlanSummarizeTable> planSummarizeTableList = new LinkedList<>();
            List<Area> areaList = new LinkedList<>();
            String[] ids = plan.getPlanSummarizeId().split(";");
            if (ids.length > 0) {
                for (String ID : ids) {
                    PlanSummarizeTable planSummarizeTable = planSummarizeTableMapper.selectByPrimaryKey(Integer.parseInt(ID));
                    planSummarizeTableList.add(planSummarizeTable);
                }
            }
            map.put("planSummarizeTableList", planSummarizeTableList);
            String[] idss = plan.getPlanAreaId().split(";");
            if (ids.length > 0) {
                for (String ID : idss) {
                    Area area = areaMapper.selectByPrimaryKey(Integer.parseInt(ID));
                    areaList.add(area);
                }
            }
            map.put("areaList", areaList);
            mapList.add(map);
        }
        return Message.success(null).add(mapList);
    }

    @Override
    public Message selectByPlanStudentId(int studentId) {
        List<Plan> planList = planMapper.selectByPlanStudentId(studentId);
        List<Map> mapList = new LinkedList<>();
        for (Plan plan : planList) {
            HashMap map = new HashMap();
            if (plan.getPlanStudentId() != null)
                map.put("planStudentName", studentMapper.selectByPrimaryKey(plan.getPlanStudentId()).getStudentName());
            else map.put("planStudentName", null);

            if (plan.getPlanTeacherleaderId() != null)
                map.put("planTeacherLeaderName", staffMapper.selectByPrimaryKey(plan.getPlanTeacherleaderId()).getStaffName());
            else map.put("planTeacherLeaderName", null);

            if (plan.getPlanRecoveryleaderId() != null)
                map.put("planRecoveryLeaderName", staffMapper.selectByPrimaryKey(plan.getPlanRecoveryleaderId()).getStaffName());
            else map.put("planRecoveryLeaderName", null);

            if (plan.getPlanPhysicianleaderId() != null)
                map.put("planPhysicianLeaderName", staffMapper.selectByPrimaryKey(plan.getPlanPhysicianleaderId()).getStaffName());
            else map.put("planPhysicianLeaderName", null);

            if (plan.getPlanPeopleId() != null)
                map.put("planPeopleName", staffMapper.selectByPrimaryKey(plan.getPlanPeopleId()).getStaffName());
            else map.put("planPeopleName", null);

            if (plan.getPlanExplainpeopleId() != null)
                map.put("planExplainPeopleName", staffMapper.selectByPrimaryKey(plan.getPlanExplainpeopleId()).getStaffName());
            else map.put("planExplainPeopleName", null);
            map.put("plan", plan);
            List<PlanSummarizeTable> planSummarizeTableList = new LinkedList<>();
            List<Area> areaList = new LinkedList<>();
            String[] ids = plan.getPlanSummarizeId().split(";");
            if (ids.length > 0) {
                for (String ID : ids) {
                    PlanSummarizeTable planSummarizeTable = planSummarizeTableMapper.selectByPrimaryKey(Integer.parseInt(ID));
                    planSummarizeTableList.add(planSummarizeTable);
                }
            }
            map.put("planSummarizeTableList", planSummarizeTableList);
            String[] idss = plan.getPlanAreaId().split(";");
            if (ids.length > 0) {
                for (String ID : idss) {
                    Area area = areaMapper.selectByPrimaryKey(Integer.parseInt(ID));
                    areaList.add(area);
                }
            }
            map.put("areaList", areaList);
            mapList.add(map);
        }
        return Message.success(null).add(mapList);
    }

    @Override
    public Message updateByPrimaryKeys(Plan plan, List<Area> areaList, List<PlanSummarizeTable> planSummarizeTableList) {
        planMapper.updateByPrimaryKeySelective(plan);
        for(Area area :areaList){
            areaMapper.updateByPrimaryKeySelective(area);
        }
        for(PlanSummarizeTable planSummarizeTable :planSummarizeTableList){
            planSummarizeTableMapper.updateByPrimaryKeySelective(planSummarizeTable);
        }
        return Message.success(null);
    }

    @Override
    public Message insertSelectives(Plan plan , List<Area> areaList , List<PlanSummarizeTable> planSummarizeTableList) {
        String areaIds = null;
        String planSummarizeTableIds = null;
        for (Area area :areaList) {
            areaMapper.insertSelective(area);
            areaIds = areaIds + area.getAreaId() + ";";
        }
        for (PlanSummarizeTable planSummarizeTable :planSummarizeTableList) {
            planSummarizeTableMapper.insertSelective(planSummarizeTable);
            planSummarizeTableIds = planSummarizeTableIds + planSummarizeTable.getPlanSummarizeTableId()+";";
        }
        plan.setPlanAreaId(areaIds);
        plan.setPlanSummarizeId(planSummarizeTableIds);
        return Message.success(null).add(planMapper.insertSelective(plan));
    }


    @Override
    public Message updateByPrimaryKeySelective(Plan plan) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        if (plan.getPlanTimeToString()!=null)
            plan.setPlanTime(simpleDateFormat.parse(plan.getPlanTimeToString()));
        if (plan.getPlanExplaintimeToString() != null) {
            plan.setPlanExplaintime(simpleDateFormat.parse(plan.getPlanExplaintimeToString()));
        }
        return planMapper.updateByPrimaryKeySelective(plan) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message updateByPrimaryKey(Plan plan) {
        return planMapper.updateByPrimaryKey(plan) > 0 ? Message.success(null) : Message.fail(null);
    }

    HashMap PlanReturnInit(Plan plan) {
        HashMap hashMap = new HashMap();
        hashMap.put("plan", plan);

        if (plan.getPlanStudentId() != null)
            hashMap.put("planStudentName", studentMapper.selectByPrimaryKey(plan.getPlanStudentId()).getStudentName());
        else hashMap.put("planStudentName", null);

        if (plan.getPlanTeacherleaderId() != null)
            hashMap.put("planTeacherLeaderName", staffMapper.selectByPrimaryKey(plan.getPlanTeacherleaderId()).getStaffName());
        else hashMap.put("planTeacherLeaderName", null);

        if (plan.getPlanRecoveryleaderId() != null)
            hashMap.put("planRecoveryLeaderName", staffMapper.selectByPrimaryKey(plan.getPlanRecoveryleaderId()).getStaffName());
        else hashMap.put("planRecoveryLeaderName", null);

        if (plan.getPlanPhysicianleaderId() != null)
            hashMap.put("planPhysicianLeaderName", staffMapper.selectByPrimaryKey(plan.getPlanPhysicianleaderId()).getStaffName());
        else hashMap.put("planPhysicianLeaderName", null);

        if (plan.getPlanPeopleId() != null)
            hashMap.put("planPeopleName", staffMapper.selectByPrimaryKey(plan.getPlanPeopleId()).getStaffName());
        else hashMap.put("planPeopleName", null);

        if (plan.getPlanExplainpeopleId() != null)
            hashMap.put("planExplainPeopleName", staffMapper.selectByPrimaryKey(plan.getPlanExplainpeopleId()).getStaffName());
        else hashMap.put("planExplainPeopleName", null);

        return hashMap;
    }


}







